<?php
#基础函数
include_once('_path.php');
include_once("../includes/init.php");

#获取页面变量
$action = trim($_GET['action']);
$id= trim($_REQUEST['id']) + 0;
	$quote['id'] = string_sql($id);
#如果有group_id
if ($id > 0) {
	$sql = "SELECT * FROM news WHERE id=$id";
	$_news = $database->queryFirst($sql);
	if (!$_news['id']) {
		$_REQUEST['referrer'] = "index.php";
		redirect("记录不存在!");
	}
}
#得到post的值

$title = trim($_REQUEST['title']);
	$quote['title'] = string_sql($title);
$content = trim($_REQUEST['content']);
	$quote['content'] = $content;
$dateline = trim($_REQUEST['dateline']);
	$quote['dateline'] = string_sql(strtotime($dateline));

#如果是编辑
if ($action == "edit.form") {
	$title = $_news['title'];
	$content = $_news['content'];
        $dateline= $_news['dateline'];
}
#如果是添加操作
if ($action == "add.do") {
	verify_field("title", "新闻标题", "empty");
        verify_field("dateline", "发布日期", "empty");
        verify_field("content", "新闻内容", "empty");

	if (!count($error_strs)) {
		$sql = "INSERT INTO news (title, content, dateline) VALUES (".$quote['title'].", '".$quote['content']."', ".$quote['dateline'].");";
		$database->execute($sql);
		$_REQUEST['referrer'] = "index.php";
		redirect("添加新闻成功!");
	} else {
		$action = "add.form";
	}
#如果是编辑操作
} else if ($action == "edit.do" && $id > 0) {

	verify_field("title", "新闻标题", "empty");
        verify_field("dateline", "发布日期", "empty");
        verify_field("content", "新闻内容", "empty");

	if (!count($error_strs)) {
		$sql = "UPDATE news SET title = ".$quote['title'].", content = '".$quote['content']."', dateline = ".$quote['dateline']." WHERE id = ".$quote['id'].";";
		$rs = $database->execute($sql);

		$_REQUEST['referrer'] = "index.php";
		redirect("修改新闻成功!");
	} else {
		$action = "edit.form";
	}
#如果是删除操作
} else if ($action == "delete.do" && $id > 0) {

	$database->execute("DELETE FROM news WHERE id = ".$quote['id'].";");
        $_REQUEST['referrer'] = "index.php";
        redirect("删除新闻成功!");
}
#样式
$css = array();
$js = array("tiny_mce/tiny_mce");
#菜单
$menu = "news";
include_once("../layout/_header.php");
?>
<div class="main">
<div class="traininc-nav-list">
<ul>
<li><a href="index.php">新闻列表</a></li>
<?php
if($action == "add.form"){
?>
<li class="selected"><a href="javascript:;">增加一条新闻</a></li>
<?php
}elseif($action == "edit.form"){
?>
<li class="selected"><a href="javascript:;">修改新闻</a></li>
<?php
}
?>
</ul>
</div>
<script language="javascript" type="text/javascript">
  tinyMCE.init({
    language : "zh-cn",
    theme : "advanced",
    mode: "exact",
    elements : "content",
    theme_advanced_toolbar_location : "top",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,"
    + "justifyleft,justifycenter,justifyright,justifyfull,formatselect,"
    + "bullist,numlist,outdent,indent",
    theme_advanced_buttons2 : "link,unlink,anchor,image,separator,"
    +"undo,redo,cleanup,code,separator,sub,sup,charmap",
    theme_advanced_buttons3 : "",
    height:"300px",
    width:"600px"
});
</script>
<?
if ($action == "add.form") {
?>
<form action="edit.php?action=add.do" method="post" id="form" name="form">
<div class="box-con">
<table class="table-input">
  				<tr>
					<td class="title">标题</td>
					<td><input name="title"  value="<?= htmlspecialchars($title) ?>" type="text" size="100"><?= show_field_error('title') ?></td>
				</tr>
				<tr>
					<td class="title">发布日期</td>
                                        <td><input type="text" name="dateline" value="<?=date(_CONST_DATE_FORMAT.' '._CONST_TIME_FORMAT,time())?>" size="30"><?= show_field_error('dateline') ?></td>
				</tr>
				<tr>
					<td class="title">新闻内容</td>
                                        <td><textarea name="content" id="content" cols="60" rows="7"><?= htmlspecialchars($content) ?></textarea><?= show_field_error('content') ?></td>
				</tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="提交" class="btnA" /><input type="reset" name="" value="取消" class="btnB" /></td>
        </tr>
</table>
</div>
</form>
<?
} else if ($action == "edit.form") {
?>
<form action="edit.php?action=edit.do" method="post" id="form" name="form">
<input type="hidden" name="id" value="<?= $id ?>">
<div class="box-con">
<table class="table-input">
  				<tr>
					<td class="title">标题</td>
					<td><input name="title"  value="<?= htmlspecialchars($title) ?>" type="text" size="100"><?= show_field_error('title') ?></td>
				</tr>
				<tr>
					<td class="title">发布日期</td>
                                        <td><input type="text" name="dateline" value="<?=date(_CONST_DATE_FORMAT.' '._CONST_TIME_FORMAT,$dateline)?>" size="30"><?= show_field_error('dateline') ?></td>
				</tr>
				<tr>
					<td class="title">新闻内容</td>
                                        <td><textarea name="content" id="content" cols="60" rows="7"><?= htmlspecialchars($content) ?></textarea><?= show_field_error('content') ?></td>
				</tr>
        <tr>
                <td class="title"> </td>
                <td><input type="submit" name="" value="提交" class="btnA" /><input type="reset" name="" value="取消" class="btnB" /></td>
        </tr>
</table>
</div>
</form>
<?php
}
?>
</div>
<?
include_once("../layout/_footer.php");
?>